<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item label>
          <el-select
            v-model="condition._area"
            placeholder="区域名称"
            @change="areaChange(condition._area)"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label>
          <el-select v-model="condition._dorm" placeholder="宿舍楼名称" clearable>
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label>
          <el-select v-model="condition._sex" placeholder="性别" clearable>
            <el-option
              v-for="item in sex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label>
          <el-select v-model="condition._person" placeholder="可住人数" clearable>
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="condition.roomNumber"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="success" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 数据展示区域 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="affiliationName" fixed min-width="150"></el-table-column>
      <!-- <el-table-column align="center" label="区域编号" prop="affiliationNumber" fixed min-width="100"></el-table-column> -->
      <!-- <el-table-column align="center" label="宿舍楼编号" prop="dormNumber" min-width="150"></el-table-column> -->
      <el-table-column align="center" label="宿舍楼名称" prop="dormName" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼栋号" prop="number" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼层" prop="layers" min-width="50"></el-table-column>
      <el-table-column align="center" label="房间号" prop="roomNumber" min-width="100"></el-table-column>
      <el-table-column align="center" label="房间类型" min-width="100">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.roomType === '0'">单人间</div>
          <div v-else-if="scope.row.roomType === '1'">多人间</div>
          <div v-else-if="scope.row.roomType === '2'">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="房间状态" min-width="100">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.roomStatus === '0'">禁用</div>
          <div v-else-if="scope.row.roomStatus === '1'">启用</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="临时" prop="isTemporary" min-width="60">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.isTemporary === '0'">否</div>
          <div v-else-if="scope.row.isTemporary === '1'">是</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入住性别" prop="roomSex" min-width="80">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.roomSex === '1'">男</div>
          <div v-else-if="scope.row.roomSex === '2'">女</div>
          <div v-else-if="scope.row.roomSex === '3'">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="费用承担" prop="roomSex" min-width="80">
        <template slot-scope="scope">
          <!-- <div>{{scope.row.roomType}}</div> -->
          <div v-if="scope.row.costBear === '1'">公司</div>
          <div v-else-if="scope.row.costBear === '2'">员工</div>
          <div v-else-if="scope.row.costBear === '2'">公摊</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="可住人数" prop="full" min-width="100"></el-table-column>
      <!-- <el-table-column align="center" label="已住人数" prop="used" min-width="100"></el-table-column> -->
      <el-table-column align="center" label="住宿费用" prop="roomCost" min-width="100"></el-table-column>
      <el-table-column align="center" label="操作" fixed="right" min-width="200">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="8rem" ref="newForm" :model="newRoom" :rules="rule">
        <el-form-item label="区域" prop="area">
          <el-select
            v-model="newRoom.area"
            placeholder
            @change="areaChange(newRoom.area)"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼" prop="dorm">
          <el-select
            v-model="newRoom.dorm"
            placeholder="宿舍楼编号"
            clearable
            @change="dormChange(newRoom.dorm)"
          >
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-select v-model="newRoom.floor" placeholder="楼层" clearable>
            <el-option
              v-for="item in floor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="newRoom.roomNumber" @blur="roomNumberCheck(newRoom.roomNumber,'new')"></el-input>
        </el-form-item>
        <el-form-item label="房间类型" prop="roomType">
          <el-radio-group v-model="newRoom.roomType" @change="newRoomTypeChange(newRoom.roomType)">
            <el-radio label="0">单人间</el-radio>
            <el-radio label="1">多人间</el-radio>
            <el-radio label="2">公共房</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="入住性别" prop="roomSex">
          <el-radio-group v-model="newRoom.roomSex" :disabled="newControl">
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
            <el-radio label="3">公共房间</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可住人数" prop="full">
          <el-select v-model="newRoom.full" placeholder="人数" clearable :disabled="newControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="柜子数量" prop="cupboard">
          <el-select v-model="newRoom.cupboard" clearable :disabled="newControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认房间状态" prop="roomStatus">
          <el-radio-group v-model="newRoom.roomStatus" :disabled="newControl">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否临时" prop="isTemporary">
          <el-radio-group v-model="newRoom.isTemporary" :disabled="newControl">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="住宿费用" prop="roomCost">
          <el-input v-model.number="newRoom.roomCost" :disabled="newControl"></el-input>
        </el-form-item>
        <el-form-item label="费用承担" prop="costBear">
          <el-radio-group v-model="newRoom.costBear" :disabled="newControl">
            <el-radio label="1">公司承担</el-radio>
            <el-radio label="2">员工承担</el-radio>
            <el-radio label="3">公摊</el-radio>
            <el-radio label="4">现结类冷水(洗衣机类型)</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="8rem" ref="alterForm" :model="alterRoom" :rules="rule">
        <el-form-item label="房间号" prop="roomNumber">
          <el-input
            v-model="alterRoom.newRoomNumber"
            @blur="roomNumberCheck(alterRoom.newRoomNumber,'alter')"
          ></el-input>
        </el-form-item>
        <el-form-item label="房间类型" prop="roomType">
          <el-radio-group
            v-model="alterRoom.roomType"
            @change="alterRoomTypeChange(alterRoom.roomType)"
          >
            <el-radio label="0">单人间</el-radio>
            <el-radio label="1">多人间</el-radio>
            <el-radio label="2">公共房</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="入住性别" prop="roomSex">
          <el-radio-group v-model="alterRoom.roomSex" :disabled="alterControl">
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
            <el-radio label="3">公共房间</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可住人数" prop="full">
          <el-select v-model="alterRoom.full" clearable :disabled="alterControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="柜子数量" prop="cupboard">
          <el-select v-model="alterRoom.cupboard" clearable :disabled="alterControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认房间状态" prop="roomStatus">
          <el-radio-group v-model="alterRoom.roomStatus" :disabled="alterControl">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否临时" prop="isTemporary">
          <el-radio-group v-model="alterRoom.isTemporary" :disabled="alterControl">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="住宿费用" prop="roomCost">
          <el-input v-model.number="alterRoom.roomCost" :disabled="alterControl"></el-input>
        </el-form-item>
        <el-form-item label="费用承担" prop="costBear">
          <el-radio-group v-model="alterRoom.costBear" :disabled="alterControl">
            <el-radio label="1">公司承担</el-radio>
            <el-radio label="2">员工承担</el-radio>
            <el-radio label="3">公摊</el-radio>
            <el-radio label="4">现结类冷水(洗衣机类型)</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="alterSubmit('alterForm')"
          :loading="alterLoading"
        >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { getArea } from "../../api/area.js";
import { getDorm, getFloor } from "../../api/dorm.js";
import {
  roomSearch,
  newRoom,
  roomCheck,
  roomDel,
  alterRoom
} from "../../api/room.js";
export default {
  data() {
    return {
      condition: {
        _area: "",
        _dorm: "",
        _sex: "",
        roomNumber: "",
        _person: "",
        page: 1
      },
      tableData: [
        {
          affiliationName: null,
          affiliationNumber: null,
          dormNumber: null,
          dormName: null,
          number: null,
          layers: null,
          roomNumber: null,
          roomStatus: null,
          isTemporary: null,
          roomSex: null,
          full: null,
          used: null,
          roomCost: null,
          costBear: null,
          roomType: null,
          cupboard: null
        }
      ],
      newRoom: {
        area: null,
        dorm: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        roomSex: null,
        full: null,
        roomStatus: null,
        isTemporary: null,
        roomCost: null,
        costBear: null,
        cupboard: null
      },
      newControl: false,
      alterRoom: {
        newRoomNumber: null,
        roomType: null,
        roomSex: null,
        full: null,
        roomStatus: null,
        isTemporary: null,
        roomCost: null,
        costBear: null,
        cupboard: null
      },
      alterControl: false,
      area: [
        {
          value: "",
          label: ""
        }
      ],
      dorm: [
        {
          value: "",
          label: ""
        }
      ],
      floor: [
        {
          value: null,
          label: null
        }
      ],
      sex: [
        {
          value: "男",
          label: "男"
        },
        {
          value: "女",
          label: "女"
        }
      ],
      series: [
        {
          value: "0",
          label: "0"
        },
        {
          value: "1",
          label: "1"
        },
        {
          value: "2",
          label: "2"
        },
        {
          value: "3",
          label: "3"
        },
        {
          value: "4",
          label: "4"
        },
        {
          value: "5",
          label: "5"
        },
        {
          value: "6",
          label: "6"
        }
      ],
      rule: {
        area: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" }
        ],
        dorm: [{ required: true, message: "请选择宿舍楼", trigger: "change" }],
        floor: [{ required: true, message: "请选择楼层", trigger: "change" }],
        roomNumber: [
          { required: true, message: "请输入房间名称", trigger: "blur" }
        ],
        roomType: [
          { required: true, message: "请选择房间类型", trigger: "change" }
        ],
        roomSex: [
          { required: true, message: "请选择入住性别", trigger: "change" }
        ],
        full: [
          { required: true, message: "请选择可住人数", trigger: "change" }
        ],
        roomStatus: [
          { required: true, message: "请选择房间状态", trigger: "change" }
        ],
        isTemporary: [
          { required: true, message: "请选择是否临时入住", trigger: "change" }
        ],
        roomCost: [
          { required: true, message: "请输入住宿费用", trigger: "blur" },
          { type: "number", message: "值必须为数字值" }
        ],
        costBear: [
          { required: true, message: "请选择费用承担方", trigger: "change" }
        ]
      },
      newFormVisible: false,
      loading: false,
      currentPage: 1,
      totalPage: 1,
      newLoading: false,
      alterFormVisible: false,
      alterLoading: false
    };
  },
  methods: {
    getArea() {
      getArea().then(res => {
        if (res) {
          this.area = res.msg;
        }
      });
    },
    // 选完区域后触发宿舍楼
    areaChange(area) {
      if (area === "") {
        return true;
      }
      // 搜索宿舍楼编号
      getDorm(area).then(res => {
        if (res) {
          // this.condition._dorm = null;
          this.dorm = res.msg;
        } else {
          this.dorm = [
            {
              value: "",
              label: ""
            }
          ];
          // this.condition._dorm = "";
        }
      });
    },
    dormChange(dorm) {
      if (dorm === "") {
        return true;
      }
      // 获取当前楼层
      getFloor(this.newRoom.area, this.newRoom.dorm).then(res => {
        if (res) {
          this.floor = [];
          let floor = res.msg[0].d_layers;
          for (let i = 1; i <= floor; i++) {
            let _floor = { value: i, label: i };
            this.floor.push(_floor);
          }
        }
      });
    },
    getData() {
      this.loading = true;
      roomSearch(this.condition).then(res => {
        this.loading = false;
        if (res) {
          this.tableData = res.msg;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    // 查询
    search() {
      this.condition.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    newRoomTypeChange(roomType) {
      if (roomType === "2") {
        this.newControl = true;
        this.newRoom.roomSex = "3";
        this.newRoom.full = "0";
        this.newRoom.roomStatus = "1";
        this.newRoom.isTemporary = "0";
        this.newRoom.roomCost = 0;
        this.newRoom.costBear = "3";
        this.newRoom.cupboard = "0";
      } else {
        this.newControl = false;
        this.newRoom.roomSex = null;
        this.newRoom.number = null;
        this.newRoom.roomStatus = null;
        this.newRoom.isTemporary = null;
        this.newRoom.roomCost = null;
        this.newRoom.costBear = null;
        this.newRoom.cupboard = null;
      }
    },
    alterRoomTypeChange(roomType) {
      if (roomType === "2") {
        this.alterControl = true;
        this.alterRoom.roomSex = "3";
        this.alterRoom.full = "0";
        this.alterRoom.roomStatus = "1";
        this.alterRoom.isTemporary = "0";
        this.alterRoom.roomCost = 0;
        this.alterRoom.costBear = "3";
        this.alterRoom.cupboard = "0";
      } else {
        this.alterControl = false;
        this.alterRoom.roomSex = null;
        this.alterRoom.number = null;
        this.alterRoom.roomStatus = null;
        this.alterRoom.isTemporary = null;
        this.alterRoom.roomCost = null;
        this.alterRoom.costBear = null;
        this.alterRoom.cupboard = null;
      }
    },
    // 新建
    newSubmit(forName) {
      this.$refs[forName].validate(val => {
        this.newLoading = true;
        if (val) {
          newRoom(this.newRoom).then(res => {
            this.newLoading = false;
            if (res) {
              this.getData();
              this.$refs[forName].resetFields();
              this.newFormVisible = false;
              this.$message({
                message: res.msg,
                type: "success"
              });
            }
          });
        }
      });
    },
    outExcel() {
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/building/Building/roomInfoExcelOut";
    },
    // 编辑
    handleEdit($index, row) {
      // 有人入住的房间无法编辑
      if (row.used > 1) {
        this.$message({
          message: "此房间有人员入住,无法修改",
          type: "warning"
        });
      } else {
        this.alterControl = false;
        this.alterFormVisible = true;
        this.alterRoom = Object.assign({}, row);
        this.alterRoom.newRoomNumber = row.roomNumber;
      }
    },
    // 确认编辑
    alterSubmit(forName) {
      this.$refs[forName].validate(val => {
        if (val) {
          alterRoom(this.alterRoom).then(res => {
            if (res) {
              this.alterFormVisible = false;
              this.$message({
                type: "success",
                message: res.msg
              });
              this.getData();
            }
          });
        }
      });
    },
    // 删除
    del(index, row) {
      if (row.used > 1) {
        this.$message({
          message: "此房间有人员入住,无法修改",
          type: "warning"
        });
      } else {
        this.$confirm("此操作将永久删除该房间下所有内容, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            this.loading = true;
            console.log(row);
            roomDel(row.affiliationNumber, row.dormNumber, row.roomNumber).then(
              res => {
                this.loading = false;
                if (res) {
                  this.$message({
                    type: "success",
                    message: res.msg
                  });
                  this.getData();
                }
              }
            );
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
        // this.alterControl = false;
        // console.log(row);
        // this.alterFormVisible = true;
        // this.alterRoom = Object.assign({}, row);
      }
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    roomNumberCheck(roomNumber, type) {
      if (type === "new") {
        if (!this.newRoom.area || !this.newRoom.dorm) {
          this.newRoom.roomNumber = "";
          this.$message.error("未选择区域或宿舍楼");
        } else {
          roomCheck(this.newRoom.area, this.newRoom.dorm, roomNumber).then(
            res => {
              if (res) {
                return true;
              } else {
                this.newRoom.roomNumber = "";
              }
            }
          );
        }
      } else if (type === "alter") {
        if (!this.alterRoom.affiliationNumber || !this.alterRoom.dormNumber) {
          this.alterRoom.roomNumber = "";
          this.$message.error("未选择区域或宿舍楼");
        } else {
          roomCheck(
            this.alterRoom.affiliationNumber,
            this.alterRoom.dormNumber,
            roomNumber
          ).then(res => {
            if (res) {
              return true;
            } else {
              this.alterRoom.roomNumber = "";
            }
          });
        }
      }
    }
  },
  mounted() {
    this.getArea();
    this.getData();
  }
};
</script>
<style>
</style>
